package com.zhaoziqian.config;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Created with fabric-cli
 * 作者： zhaoziqian
 * 时间： 2018/5/29  上午9:11
 * 功能： 定义系统中需要获取的环境变量值
 */
@Component
public class EnvParam {


    public static String ORG;
    public static String PEER;
    public static String ORDERERID;
    public static String ORDERER_ORG;
    public static String CHAINCODE_ROOT_DIR;
    public static Integer PROPOSAL_WAIT_TIME;
    public static String GRPCS_WAIT_TIME;

    private static final Logger logger = LoggerFactory.getLogger(EnvParam.class);

    @PostConstruct
    private void init() {
        String org = System.getenv("org");
        ORG = (StringUtils.isBlank(org)) ? "org1" : org;
        String peer = System.getenv("peer");
        PEER = (StringUtils.isBlank(peer)) ? "peer1" : peer;
        String ordererId = System.getenv("ordererId");
        ORDERERID = (StringUtils.isBlank(ordererId)) ? "orderer1" : ordererId;
        String ordererOrg = System.getenv("ordererOrg");
        ORDERER_ORG = (StringUtils.isBlank(ordererOrg)) ? "org1" : ordererOrg;
        String chaincodeRootDir = System.getenv("CHAINCODE_ROOT_DIR");
//        CHAINCODE_ROOT_DIR = (StringUtils.isBlank(chaincodeRootDir)) ? "/Users/zhaoziqian/Desktop/network_resources/chaincode" : chaincodeRootDir;
        CHAINCODE_ROOT_DIR = (StringUtils.isBlank(chaincodeRootDir)) ? "/Users/zhaoziqian/i/app/fabric-java-sdk/chaincode" : chaincodeRootDir;
        String waitTimeStr = System.getenv("PROPOSAL_WAIT_TIME");
        Integer proposalWaitTime = 0;
        if (!StringUtils.isBlank(waitTimeStr)) {
            proposalWaitTime = Integer.parseInt(System.getenv("PROPOSAL_WAIT_TIME"));
        }
        PROPOSAL_WAIT_TIME = proposalWaitTime <= 1000 ? 1000 : proposalWaitTime;
        String grpcsWaitTime = System.getenv("GRPCS_WAIT_TIME");
        GRPCS_WAIT_TIME = (StringUtils.isBlank(grpcsWaitTime)) ? "10000" : grpcsWaitTime;

        logger.info("-------------- Environment variable ---------------");
        logger.info(">>     ORG : {}", ORG);
        logger.info(">>     PEER : {}", PEER);
        logger.info(">>     ORDERERID : {}", ORDERERID);
        logger.info(">>     ORDERER_ORG : {}", ORDERER_ORG);
        logger.info(">>     CHAINCODE_ROOT_DIR : {}", CHAINCODE_ROOT_DIR);
        logger.info(">>     PROPOSAL_WAIT_TIME : {}", PROPOSAL_WAIT_TIME);
        logger.info(">>     GRPCS_WAIT_TIME : {}", GRPCS_WAIT_TIME);
        logger.info("-------------- ^^^^^^^^^^^^^^^^^^^^ ---------------");


    }

}
